<script setup>
import { ref } from 'vue'
import '@/assets/common/el-form-label-center.scss'
import { UploadOne } from '@icon-park/vue-next'
import { successMsg, warningMsg } from '@/utils/sendMsgUtils.js'
import { musicUploadService } from '@/api/music.js'
import { useRouter } from 'vue-router'

const router = useRouter()
const music = ref({
  name: '',
  artist: '',
  lyrics: ''
})
const loading = ref(false)

// 校验规则
const rules = ref({
  name: [],
  artist: [],
  lyrics: []
})

const formRef = ref()
const uploadRef = ref()
const audioFile = ref(null)

const onSubmit = async () => {
  formRef.value?.validate(async (valid) => {
    if (valid) {
      if (audioFile.value == null) {
        warningMsg('文件内容为空，请上传文件')
        return
      }
      const formData = new FormData()
      loading.value = true
      // 将表单数据添加到 FormData 中
      formData.append('name', music.value.name)
      formData.append('artist', music.value.artist)
      formData.append('lyrics', music.value.lyrics)

      formData.append('audio', audioFile.value)
      const resp = await musicUploadService(formData)
      if (resp.code === 1) {
        successMsg(resp.data)
        music.value = {}
        audioFile.value = null
        loading.value = false
        uploadRef.value.clearFiles()
      }
    } else {
      console.log('添加失败')
    }
  })

}
const handleChange = (fileList) => {
  if (fileList.length === 0) {
    audioFile.value = null
    return
  }
  audioFile.value = fileList.raw

}
</script>

<template>
  <el-form
    v-loading="loading"
    :model="music"
    :rules="rules"
    element-loading-text="正在上传..."
    ref="formRef"
    label-width="auto"
    class="demo-ruleForm"
  >
    <el-form-item label="歌曲名：" prop="name">
      <el-input placeholder="请输入歌曲名" style="width: 300px" v-model="music.name" />
    </el-form-item>
    <el-form-item label="歌手：" prop="artist">
      <el-input placeholder="请输入歌曲歌手" style="width: 300px" v-model="music.artist" />
    </el-form-item>
    <el-form-item label="歌词：" prop="lyrics">
      <el-input :rows="5" type="textarea" placeholder="请输入歌曲歌词" style="width: 300px" v-model="music.lyrics" />
    </el-form-item>
    <el-form-item label="音频文件：">
      <el-upload
        :limit="1"
        class="upload-demo"
        drag
        :auto-upload="false"
        accept=".mp3"
        :on-change="handleChange"
        :multiple="false"
        ref="uploadRef"
      >
        <el-icon class="el-icon--upload">
          <upload-one theme="outline" size="18" fill="#5e616d" />
        </el-icon>
        <div class="el-upload__text"><em>拖入或点击</em>框内进行上传音频文件</div>
        <template #tip>
          <div class="el-upload__tip">
            传输文件要求要.mp3文件，<br />歌名以及作者会优先获取MP3里面的命名
          </div>
        </template>
      </el-upload>
    </el-form-item>
    <div style=" text-align: center;margin-bottom: 15px">
      <el-button style="width: 100px" size="large" round @click="router.go(-1)">返回</el-button>
      <el-button style="width: 200px" size="large" round type="primary" @click="onSubmit">上传音乐</el-button>
    </div>
  </el-form>
</template>

<style scoped>
.demo-ruleForm {
  max-width: 400px;
  margin: 50px auto;

  .upload-demo {
    width: 100%;
    max-width: 800px;
  }
}
</style>
